package com.elock.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.elock.base.core.Filter;
import com.elock.base.core.Page;
import com.elock.base.core.Pageable;
import com.elock.base.core.controller.BaseController;
import com.elock.base.core.controller.ResultMsg;
import com.elock.base.core.util.JsonUtils;
import com.elock.entity.SystemParameter;
import com.elock.service.AdminSystemParameterBaseService;

/**
 * Controller - 系统参数
 * @author 仁德
 */
@Controller("AdminSystemParamController")
@RequestMapping({ "/adminSystemParameter" })
public class AdminSystemParamController extends BaseController {

	@Resource(name = "adminSystemParameterBaseServiceImpl")
	private AdminSystemParameterBaseService adminSystemParameterService;

	@RequestMapping(value = "/list_tb", method = RequestMethod.GET)
	public String list_tb(ModelMap model) {

		return "/adminSystemParameter/list_tb";
	}

	/**
	 * 列表
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(Pageable pageable, ModelMap model) {

		return "/adminSystemParameter/list";
	}

	/**
	 * 列表数据
	 */
	@RequestMapping(value = "/list_data", method = RequestMethod.POST)
	@ResponseBody
	public ResultMsg list_data(String code, String name, String value, Pageable pageable,
			ModelMap model) {

		List<Filter> filters = new ArrayList<Filter>();
		filters.add(Filter.isNull("companyInfoId"));
		if (StringUtils.isNotEmpty(code)) {
			filters.add(Filter.like("code", "%" + code + "%"));
		}
		if (StringUtils.isNotEmpty(name)) {
			filters.add(Filter.like("name", "%" + name + "%"));
		}
		if (StringUtils.isNotEmpty(value)) {
			filters.add(Filter.like("value", "%" + value + "%"));
		}
		Page<SystemParameter> page = this.adminSystemParameterService.findPage(filters,
				null,
				pageable);
		String jsonPage = JsonUtils.toJson(page);
		return ResultMsg.success(jsonPage);
	}

	/**
	 * 编辑
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public String edit(Long id, ModelMap model) {

		SystemParameter systemParam = (SystemParameter) this.adminSystemParameterService
				.find(id);

		SystemParameter adminSystemParam = this.adminSystemParameterService
				.getAdminParam(systemParam.getCode());
		boolean isModify = false;
		if ((adminSystemParam == null)
				|| (adminSystemParam.getIsModify().booleanValue())) {
			isModify = true;
		}

		model.addAttribute("isModify", Boolean.valueOf(isModify));
		model.addAttribute("systemParam", systemParam);

		return "/adminSystemParameter/edit";
	}

	/**
	 * 更新
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public ResultMsg update(SystemParameter systemParameter, HttpServletRequest request,
			RedirectAttributes redirectAttributes) {

		this.adminSystemParameterService.updateSystemParameter(systemParameter);

		return success();
	}
}
